package com.ctg.itrdc.sysmgr.portal.control;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.sysmgr.portal.api.dto.FuncCompDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.IFuncCompService;
import com.ctg.itrdc.sysmgr.portal.common.entity.CommonResult;
import com.ctg.itrdc.sysmgr.portal.utils.DTOHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

@Controller
@RequestMapping("/func_comp")
public class FuncCompController extends SysmgrCURDController<Long, FuncCompDTO> {

	@Resource
	private IFuncCompService funcCompService;
	
	/**
	 * @api {get} /func_comp/priv_grant_query 授权查询
	 * @apiGroup func_comp
	 * @apiParam {Long} menuId 
	 * @apiParam {String} grantObjType 
	 * @apiParam {Long} grantObjId 
	 * @apiName all
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/priv_grant_query")
	@ResponseBody
	public CommonResult privGrantQuery(
			FuncCompDTO dto,
			@RequestParam(required = false) String grantObjType,
			@RequestParam(required = false) Long grantObjId){
		List<FuncCompDTO> resultList = funcCompService.selectCompList(grantObjType,grantObjId,dto);
		return new CommonResult(HttpStatus.OK.toString(), "查询成功", Sets.newHashSet(resultList));
	}
	
	
	@RequestMapping("/insert")
	@ResponseBody
	public CommonResult insert(@Valid FuncCompDTO dto,BindingResult result) throws Exception {
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		return new CommonResult(HttpStatus.OK.toString(), "新增数据成功",funcCompService.insert(dto));
	}

	@RequestMapping("/update")
	@ResponseBody
	public CommonResult updateByPrimaryKey(@Valid FuncCompDTO dto,BindingResult result) throws Exception {
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		funcCompService.updateByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "更新数据成功");
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public CommonResult deleteByPrimaryKey(FuncCompDTO dto) throws Exception {
		funcCompService.deleteByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "删除成功");
	}
	
	/**
	 * 批量删除，不推荐使用
	 * 
	 * @param ids
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/batch/delete")
	@ResponseBody
	public CommonResult batchDeleteByPrimaryKey(String ids,FuncCompDTO dto) throws Exception{
		List<FuncCompDTO> dtoList = Lists.newArrayList();
		String[] idStr = ids.split(",");
		for (String i : idStr) {
			Long id = Long.parseLong(i);
			FuncCompDTO tmp = new FuncCompDTO();
			tmp.setCompId(id);
			dtoList.add(tmp);
		}
		funcCompService.batchDeleteByPrimaryKey(dtoList);
		return new CommonResult(HttpStatus.OK.toString(), "删除成功");
	}
}
